package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.mapper.DestinationMapper;
import cn.wolfcode.luowowo.article.query.DestinationQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DestinationServiceImpl implements IDestinationService {

    @Autowired
    private DestinationMapper destinationMapper;
    @Override
    public List<Destination> getDestByDeep(int deep) {
        return destinationMapper.selectByDeep(deep);
    }

    @Override
    public List<Destination> queryRefByIds(Long[] refIds) {

        return destinationMapper.selectRefByIds(refIds);
    }

    @Override
    public PageInfo<Destination> listByPage(DestinationQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo<>(destinationMapper.selectBypage(qo));
    }

    @Override
    public void deleteById(Long id) {
        destinationMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void changeHotValue(Long id, boolean hot) {
        destinationMapper.updateByHot(id, hot);
    }

    @Override
    public void updateInfoById(Long id, String info) {
        destinationMapper.updateInfoById(id, info);
    }

    @Override
    public Destination queryRegionById(Long id) {
        return destinationMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Destination> creatToasts(Long id) {
        List<Destination> toasts = new ArrayList<>();
        addToasts(toasts, id);
        Collections.reverse(toasts);
        return toasts;
    }

    @Override
    public List<Destination> listByParentId(Long parentId) {
        return destinationMapper.selectByParentId(parentId);
    }

    @Override
    public Destination getCountry(Long id) {
        List<Destination> destinations = this.creatToasts(id);
        if (destinations != null && destinations.size() > 0) {
            return destinations.get(0);
        }
        return null;
    }

    @Override
    public Destination getProvince(Long id) {
        List<Destination> destinations = this.creatToasts(id);

        if (destinations != null && destinations.size() > 1) {
            return destinations.get(1);
        }
        return null;
    }

    @Override
    public List<Destination> list() {
        return destinationMapper.selectAll();
    }


    private void addToasts(List<Destination> list, Long id) {
        Destination destination = destinationMapper.selectByPrimaryKey(id);
        if (destination == null){
            return;
        }
        list.add(destination);
        if (destination.getParent() != null) {
            addToasts(list, destination.getParent().getId());
        }
    }

}
